'''
Author: your name
Date: 2021-04-20 10:26:47
LastEditTime: 2021-05-13 17:30:21
LastEditors: Please set LastEditors
Description: In User Settings Edit
FilePath: /tinyms/tinyms-api/apps/auth/organ/service.py
'''
import json
from app import db
from .model import Organ

class OrganService():
    '''
    查询所有机构列表
    '''
    def list_all(organ_parent):
        if organ_parent !=None:
            Organs = Organ.query.filter(Organ.organ_parent==organ_parent).all()
        return Organs
    
    def list():
        # Organs = Organ.query.filter(Organ.organ_pk==organ_pk).all()
        Organs = Organ.query.filter().all()
        return Organs
    
    def save(organ):
        db.session.add(organ)
        db.session.commit()
    
    def update(organ):
        tmp_notice = Organ.query.get(organ.organ_pk)
        db.session.commit()

   #根据主键删除信息
    def delete(organ_pk):
        organ = Organ.query.get(organ_pk)
        db.session.delete(organ)
        db.session.commit()#必须要有db.session.commit,否则数据无法删除

    #获取机构条数
    def count_organ():
        count = Organ.query.count()
        return count